package com.blueskykong.tm.server.configuration.redis;

import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Map;

public class RedisClusterConfig {

    @Autowired
    private RedisProperties redisProperties;


    @Bean
    public RedisClusterConfiguration getClusterConfiguration() {
        Map<String, Object> source = Maps.newHashMap();
        source.put("spring.redis.cluster.nodes", redisProperties.getClusterNodes());
        source.put("spring.redis.cluster.max-redirects", redisProperties.getMaxRedirects());
        return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));

    }

    @Bean
    public JedisPoolConfig getJedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
        jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
        jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
        return jedisPoolConfig;
    }

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory(getClusterConfiguration(),getJedisPoolConfig());
    }


    @Bean
    public JedisClusterConnection getJedisClusterConnection() {
        return (JedisClusterConnection) jedisConnectionFactory().getConnection();
    }

    @Bean
    public RedisTemplate getStringRedisTemplate() {
        StringRedisTemplate clusterTemplate = new StringRedisTemplate();
        clusterTemplate.setConnectionFactory(jedisConnectionFactory());
        clusterTemplate.setKeySerializer(new StringRedisSerializer());
        clusterTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        return clusterTemplate;
    }


}
